﻿#include "resultmanager.h"
#include <QFile>
#include <QTextStream>
#include <QFileInfo>
#include <QDebug>
#include <QCoreApplication>

ResultManager::ResultManager()
{

}

void ResultManager::SaveResultToLogFile(QString filePath, ResultManager::RESULT_DEF res)
{
    QFileInfo fileInfo(filePath);
    bool bExist = fileInfo.isFile();

    QFile file(filePath);
    file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
    QTextStream text_stream(&file);

    if(!bExist)
    {
       text_stream << "Date    "<<"Time     " <<"res    "<<"version"<<endl;
     //  text_stream <<endl;
    }

	/*if (!res.people.isEmpty())
	{
		text_stream << res.people << " ";
	}
	else
	{
		text_stream << "people" << " ";
	}

	if (!res.product.isEmpty())
	{
		text_stream << res.product << " ";
	}
	else
	{
		text_stream << "product" << " ";
	}*/

    text_stream<<res.time.toString("yyyy-MM-dd")<<" ";
    text_stream<<res.time.toString("hh:mm:ss")<<" ";
    text_stream<<res.result<<" ";
    text_stream << QCoreApplication::applicationVersion() << " ";

    text_stream <<endl;
    file.flush();
    file.close();
}

QMap<QString, ResultManager::DATE_RESULT_DEF> ResultManager::LogsByDate(QString filePath)
{
	QMap<QString, ResultManager::DATE_RESULT_DEF> res;
	QFileInfo fileInfo(filePath);
	bool bExist = fileInfo.isFile();

	if (!bExist)
	{
		return res;
	}

	QFile file(filePath);
	if (file.open(QIODevice::ReadOnly | QIODevice::Text))
	{
		int index = 0;
		while (!file.atEnd())
		{
			QByteArray t = file.readLine();
			QString line(t);
			QStringList list = line.split(" ");
			if (index != 0 && list.length() >= 3)
			{

				ResultManager::DATE_RESULT_DEF r = res[list.at(0)];
				

			//	QString strBuffer = QString("%1 %2").arg(list.at(0)).arg(list.at(1));
			//	r.time = QDateTime::fromString(strBuffer, "yyyy-MM-dd hh:mm:ss");
				/*	r.people = list.at(0);
					r.product = list.at(1);

	*/
				QString str = list.at(2);
				int rr = str.toInt();
				
				if (rr == 1)
				{
					r.ok++;
				}
				else {
					r.ng++;
					switch (rr)
					{
				/*	case ERR_CROSS_MARK:
						r.mark.iCrossMark++;
						break;*/
					case ERR_BALL_DAMAGE:
						r.mark.iBallDamage++;
						break;
					case ERR_SUBSTRATE_DAMAGE:
						r.mark.iSubstrateDamage++;
						break;
					case ERR_CAP_DAMAGE:
						r.mark.iCap++;
						break;
					default:
						break;
					}
					
				}
                r.total++;
                if (list.length() == 3)
                {
                    r.version = "--";
                }
                else {
                    r.version = list.at(3).trimmed();
                    if (r.version.count() == 0)
                    {
                        r.version = "--";
                    }
                }

				res[list.at(0)] = r;
			}
			index++;
		}
		file.close();

	}

	return res;
}

QVector<ResultManager::RESULT_DEF> ResultManager::ParseLogFile(QString filePath)
{
	QVector<ResultManager::RESULT_DEF> res;
    QFileInfo fileInfo(filePath);
    bool bExist = fileInfo.isFile();

    if(!bExist)
    {
       return res;
    }

    QFile file(filePath);
    if (file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        int index = 0;
        while(!file.atEnd())
        {
            QByteArray t = file.readLine();
            QString line(t);
            QStringList list = line.split(" ");
            if(index != 0 && list.length() >= 3)
            {

                ResultManager::RESULT_DEF r;
				QString strBuffer = QString("%1 %2").arg(list.at(0)).arg(list.at(1));
                r.time = QDateTime::fromString(strBuffer, "yyyy-MM-dd hh:mm:ss");
			/*	r.people = list.at(0);
				r.product = list.at(1);
*/
				QString str = list.at(2);
				r.result = str.toInt();
                res.push_front(r);
            }
            index++;
        }
        file.close();

    }

    return res;
}

